﻿$(document).ready(function () {

    //$("#btnSavePatient").click(function () {
    //    savePatient();
    //});

    $("#btnAddInsurance").click(function () {
        $("#modCreateInsurance").modal("show");
    });

    $("#btnSaveInsurance").click(function () {
        saveInsurance();
        $("#modCreateInsurance").modal("hide");
    });

});


function savePatient() {

    var url = 'SavePatient';

    var Patient = JSON.stringify({
        'FName': $('#tbFName').val(),
        'LName': $('#tbLName').val(),
        'DateOfBirth': $('#tbDateOfBirth').val(),
        'SSN': $('#tbSSN').val(),
    });

    $.ajax({
        url: url,
        data: { patientJSON: Patient },
        type: 'post',
        dataType: 'json',
        success: function (data) {
            alert("The patient was saved!");
        },
        error: function (data) {
            alert("The patient was not saved, an error occured :(");
        }
    });
}

function saveInsurance() {

    var PatientInsuranceObject = {
        'InsuranceName': $('#tbInsuranceName').val(),
        'Address1': $('#tbAddress1').val(),
        'Address2': $('#tbAddress2').val(),
        'City': $('#tbCity').val(),
        'State': $('#tbState').val(),
        'ZIP': $('#tbZIP').val(),
        'MainPhone': $('#tbMainPhone').val(),
        'Fax': $('#tbFax').val(),
        'Notes': $('#tbNotes').val(),
        'PolicyNumber': $('#tbPolicyNumber').val(),
        'GroupNumber': $('#tbGroupNumber').val(),
        'NetworkNumber': $('#tbNetworkNumber').val(),
        'CoverageDate': $('#tbCoverageDate').val(),
        'PatientID': $('#hdnPatientID').val(),
    };


    var url = "Patient/AddInsurance";

    var jsonObject = JSON.stringify(PatientInsuranceObject);

    $.ajax({
        url: url,
        data: { patientInsuranceJSON: jsonObject },
        type: 'post',
        dataType: 'json',
        success: function (data) {
            var tr = "<tr data-patientinsuranceid='" + data.PatientInsuranceID + "'><td>" + data.InsuranceName + "</td><td>" + data.PolicyNumber + "</td></tr>";
            $("#tblInsurance tbody").append(tr);
        },
        error: function (data) {
            alert("Mercy...Something broke saving that Insurance...");
        }
    });

}